﻿using BaseAuthRole.AspNetCore;
using log4net;
using RS.BaseModel.Common;
using TPWorkPlat.DTO.Input;
using TPWorkPlat.Entity;
using TPWorkPlat.IService;

namespace TPWorkPlat.Service
{
    public class TestService: ITestService
    {
        private IFreeSql _IfreeSql;

        private ILog logger = log4net.LogManager.GetLogger("TestService");

        private readonly IBaseAuthClient _baseAuthClient;

        public TestService(IFreeSql freeSql, IBaseAuthClient baseAuthClient) {

            _IfreeSql = freeSql;
            _baseAuthClient= baseAuthClient;
        }

        public CommonResult<TestEntity> Get(string id)
        {

            var result = new CommonResult<TestEntity>();


            var query= this._IfreeSql.Select<TestEntity>().Where(x => x.Id == id);

            long count=0;

            var info= query.Count(out count).Page(1,1).ToList();

            result.IsError = false;

            result.Result = info.FirstOrDefault<TestEntity>();

            return result;

        }

        public CommonResult Add(TestAddInput input) {
           
            var result = new CommonResult();


            logger.Debug("日志");

            //由ApiAuthCheckFilter自动填充，直接获取就行
            var uid=input.Uid;

             //获取当前用户
            var currentUserInfo = _baseAuthClient.GetUserInfoByUid(uid).Result;
                        

            var test = new TestEntity
            {
                Age = input.Age,
                Name = input.Name,
            };

           var r=this._IfreeSql.Insert(test).ExecuteAffrows();

            if (r > 0)
            {
                result.IsError = false;
            }
            else {

                result.Message = "操作失败";
            }

            return result;

        }
    }
}